package com.sample.transaction.atomikos.db2.service.impl;

import com.sample.transaction.atomikos.db2.entity.User2;
import com.sample.transaction.atomikos.db2.repo.User2Repository;
import com.sample.transaction.atomikos.db2.service.User2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
@Transactional("secondTransactionManager")
public class User2ServiceImpl implements User2Service {

    @Autowired
    private User2Repository user2Repository;

    @Override
    public Optional<User2> findById(int id) {
        return user2Repository.findById(id);
    }

    @Override
    public User2 updateUser(User2 user, boolean throwEx) {
        User2 userNew = user2Repository.save(user);
        if(throwEx){
            throw new RuntimeException("throw a ex");
        }
        return userNew;
    }

}
